/**   
 * @Title: CodeFilesAdapter.java 
 * @Package: com.rename.icode.view 
 * @Description: Administrator版权所有
 * @author Administrator  
 * @date 2015-3-9 下午1:19:22 
 * @version  
 */

package com.rename.icode.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.rename.icode.R;
import com.rename.icode.model.CodeFile;
import com.rename.icode.utils.LogUtil;

import java.util.List;

/**
 * @Description 代码文件列表适配器
 * @author Administrator
 * @date 2015-3-9 下午1:19:22
 * @version V1.0
 */

public class CodeFilesAdapter extends BaseAdapter {

	private static final String TAG = "CodeFilesAdapter";

	public static final int SHOW_MODE = 1;
	public static final int SELECT_MODE = 2;

	private List<CodeFile> list;
	private LayoutInflater layoutinfater;
	private int flag;
	private Context context;
	private boolean[] is_select;
	private OnClickListener fileOnClick;

	public CodeFilesAdapter(Context context, List<CodeFile> list, int flag) {
		this.list = list;
		this.flag = flag;
		this.context = context;
		this.layoutinfater = LayoutInflater.from(context);

		if (flag == SELECT_MODE) {
			// 选择框初始化为未选中
			this.is_select = new boolean[list.size()];// list变化，is_select.length不会变
			setFalse();
			LogUtil.i(TAG, "构造函数is_select size is " + is_select.length);
		}
	}

	private void setFalse() {
		for (int i = 0; i < is_select.length; i++) {
			is_select[i] = false;
		}
	}

	public void updateSelection(int p, boolean selected) {
		is_select[p] = selected;
	}

	public void updateSelection(CodeFile codeFile, boolean selected) {
		if (list.contains(codeFile)) {
			int p = list.indexOf(codeFile);
			is_select[p] = selected;
		}
	}

	/**
	 * Description
	 * 
	 * @return
	 * @see android.widget.Adapter#getCount()
	 */

	@Override
	public int getCount() {
		return list.size();
	}

	/**
	 * Description
	 * 
	 * @param position
	 * @return
	 * @see android.widget.Adapter#getItem(int)
	 */

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	/**
	 * Description
	 * 
	 * @param position
	 * @return
	 * @see android.widget.Adapter#getItemId(int)
	 */

	@Override
	public long getItemId(int position) {
		return position;
	}

	/**
	 * Description
	 * 
	 * @param position
	 * @param convertView
	 * @param parent
	 * @return
	 * @see android.widget.Adapter#getView(int, android.view.View,
	 *      android.view.ViewGroup)
	 */

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		CodeFile codeFile = (CodeFile) getItem(position);

		if (flag == SHOW_MODE) {// 显示模式
			View view = null;
			ViewHolderShow holder = null;
			if (convertView == null) {
				view = layoutinfater.inflate(R.layout.file_list_item, null);
				holder = new ViewHolderShow();
				holder.text = (TextView) view.findViewById(R.id.file_name_txt);
				holder.tvLetter = (TextView) view.findViewById(R.id.type_txt);
				holder.ivDivider = (ImageView) view
						.findViewById(R.id.type_divide_img);
				holder.tvDetail = (TextView) view
						.findViewById(R.id.file_detail_txt);
				holder.tvTopBlank = (TextView) view
						.findViewById(R.id.top_blank_txt);
				view.setTag(holder);
			} else {
				view = convertView;
				holder = (ViewHolderShow) view.getTag();
			}
			holder.text.setTag(codeFile);
			// CodeFile codeFile = (CodeFile) getItem(position);

			holder.text.setText(codeFile.getName());
			// holder.tvDetail.setText(codeFile.getDetail());
			holder.tvDetail.setText("");

			LogUtil.i(TAG, "postion = " + position);
			if (position == getPositionForType(position)) {
				LogUtil.i(TAG, "show type");
				holder.tvLetter.setVisibility(View.VISIBLE);
				holder.tvLetter.setText(codeFile.getType());
				holder.ivDivider.setVisibility(View.VISIBLE);
			} else {
				LogUtil.i(TAG, "no show type");
				holder.tvLetter.setVisibility(View.GONE);
				holder.ivDivider.setVisibility(View.GONE);
			}

			if (fileOnClick != null) {
				view.setOnClickListener(fileOnClick);
			}

			return view;

		} else if (flag == SELECT_MODE) {// 选择模式
			View view = null;
			ViewHolderSelect holder = null;
			if (convertView == null) {
				view = layoutinfater.inflate(R.layout.file_list_item, null);
				holder = new ViewHolderSelect();
				holder.text = (TextView) view.findViewById(R.id.file_name_txt);
				holder.tvLetter = (TextView) view.findViewById(R.id.type_txt);
				holder.ivDivider = (ImageView) view
						.findViewById(R.id.type_divide_img);
				holder.tvDetail = (TextView) view
						.findViewById(R.id.file_detail_txt);
				holder.tvTopBlank = (TextView) view
						.findViewById(R.id.top_blank_txt);
				holder.is_select = (ImageView) view
						.findViewById(R.id.is_select_img);
				view.setTag(holder);
			} else {
				view = convertView;
				holder = (ViewHolderSelect) view.getTag();
			}

			holder.text.setTag(codeFile);
			// CodeFile codeFile = (CodeFile) getItem(position);

			holder.text.setText(codeFile.getName());
			// holder.tvDetail.setText(codeFile.getDetail());

			if (position == getPositionForType(position)) {
				holder.tvLetter.setVisibility(View.VISIBLE);
				holder.tvLetter.setText(codeFile.getType());
				holder.ivDivider.setVisibility(View.VISIBLE);
			} else {
				holder.tvLetter.setVisibility(View.GONE);
				holder.ivDivider.setVisibility(View.GONE);
			}

			holder.is_select.setVisibility(View.VISIBLE);
			LogUtil.i(TAG, "postion = " + position);
			LogUtil.i(TAG, "select mod");
			LogUtil.i(TAG, "select size = " + is_select.length);
			LogUtil.i(TAG, "list size = " + list.size());
			if (is_select[position]) {
				holder.is_select.setImageResource(R.drawable.selected);
			} else {
				holder.is_select.setImageResource(R.drawable.unselect);
			}

			if (fileOnClick != null) {
				view.setOnClickListener(fileOnClick);
			}

			return view;
		}

		return null;
	}

	/**
	 * @Description 根据文件顺序确定某一项显示文件类型
	 * @author Administrator
	 * @return
	 */
	private int getPositionForType(int postion) {
		int num = list.get(postion).getOrderNumber();
		LogUtil.i(TAG, "getPositionForType num = " + num);
		if (num == 0) {// 如果文件排在第一位
			return postion;
		}

		return -1;
	}

	/**
	 * @Description 刷新列表
	 * @author Administrator
	 * @param list
	 */
	public void refreshList(List<CodeFile> list) {
		this.list = list;
		if (this.flag == SELECT_MODE && this.is_select.length == 0) {// 如果一开始文件列表没有加载好，数量为0时，刷新时再重新创建is_select数组
			this.is_select = new boolean[list.size()];
		}
		notifyDataSetChanged();
	}

	class ViewHolderShow {

		TextView text;
		TextView tvLetter;
		TextView tvTopBlank;
		ImageView ivDivider;
		TextView tvDetail;
	}

	class ViewHolderSelect {

		TextView text;
		TextView tvLetter;
		TextView tvTopBlank;
		ImageView ivDivider;
		TextView tvDetail;
		ImageView is_select;
	}

	// 对外暴露点击事件接口
	public void setOnClickListener(OnClickListener fileOnClick) {

		this.fileOnClick = fileOnClick;
	}

}
